﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;              //命名空间
using System.Data.SqlClient;    //数据库头文件
using System.Data.Sql;         //数据库头文件
using DATA_operate_;          //自定义数据库操作类引用
using System.Text;

public partial class design_page_teacher_Default : System.Web.UI.Page
{
    DATA_operate data_operate_in_teacher_allhomework = new DATA_operate();   //创建数据库操作类对象

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            HttpCookie cookie = Request.Cookies["teaid"];
            string str_Tid = cookie.Value.ToString();    //获得教师ID
            string sql_show_allclass_homework = @"SELECT b._Hid AS 作业ID,a._mHname,a._mHupdatetime AS 作业发布时间, a._Cid AS 班级ID,A._Cname,c.班级总人数, c.班级总人数 - b.上交人数 AS 未上交人数 
			     FROM (SELECT DISTINCT _main_homework._mHid, D._Cid, _main_homework._mHupdatetime, _teacher._Tid ,_main_homework._mHname,D._Cname
			     FROM _class AS D INNER JOIN _class_homework ON D._Cid = _class_homework._Cid 
			     INNER JOIN _main_homework ON _class_homework._Hid = _main_homework._mHid 
			     INNER JOIN _teacher ON _main_homework._mHTid = _teacher._Tid 
			     WHERE (_teacher._Tid = '"+str_Tid+@"')) AS a 
			     INNER JOIN 
			     (
			     SELECT _Hid, _Cid, COUNT(_Hfinishtime) AS 上交人数 
			     FROM (
			        SELECT DISTINCT _student_homework._Hid, _class_homework_1._Cid, _student_homework._Hfinishtime, _student_homework._Sid 
			        FROM _student_homework INNER JOIN _student ON _student_homework._Sid = _student._Sid 
			        INNER JOIN _class ON _student._SCid = _class._Cid 
			        INNER JOIN _class_homework AS _class_homework_1 ON _class._Cid = _class_homework_1._Cid 
			        WHERE (_student_homework._Hfinishtime IS NULL and _student_homework._Hid not in (select _student_homework._Hid from _student_homework where _student_homework._Hfinishtime is not null))
			        
			     ) AS f 
			     GROUP BY _Hid, _Cid 
			     UNION 
			     SELECT _Hid, _Cid, COUNT(_Hfinishtime) AS 上交人数 
			     FROM 
			     (SELECT DISTINCT _student_homework_1._Hid, _class_homework_1._Cid, _student_homework_1._Hfinishtime, _student_homework_1._Sid 
			     FROM _student_homework AS _student_homework_1 INNER JOIN _student AS _student_2 ON _student_homework_1._Sid = _student_2._Sid 
			     INNER JOIN _class AS _class_2 ON _student_2._SCid = _class_2._Cid INNER JOIN _class_homework AS _class_homework_1 ON _class_2._Cid = _class_homework_1._Cid 
			     WHERE (_student_homework_1._Hfinishtime IS not NULL)
			     ) AS g 
			     GROUP BY _Hid, _Cid) AS b ON b._Hid = a._mHid AND b._Cid = a._Cid 
			     
			     INNER JOIN 
			     (SELECT DISTINCT COUNT(_student_1._Sid) AS 班级总人数, _class_1._Cid FROM _class AS _class_1 
			        INNER JOIN _student AS _student_1 ON _class_1._Cid = _student_1._SCid GROUP BY _class_1._Cid) AS c ON c._Cid = a._Cid
			        select * from _student_homework";//构造sql语句
            data_operate_in_teacher_allhomework.Data_bind(sql_show_allclass_homework, GridView_all_class_homework, "作业ID");
        }
        if(TextBox2.Text=="")//没内容不能打分
        {
            Button2.Enabled = true;
        }
    }
    protected void GridView_all_RowCommand(object sender, GridViewCommandEventArgs e)   //事件
    {
        if (e.CommandName == "gridview_choose_detail_homework")    //选择进入学生的详细事件
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow gvr = GridView_all_class_homework.Rows[index];
            string str_homeworkid = ((Label)gvr.FindControl("dLabel_homeworkid1")).Text;    //作业ID
            string str_classid = ((Label)gvr.FindControl("dLabel_homework_classid")).Text;//班级id

            extren_classuid.Text = str_classid;
            extren_classhomeworkid.Text = str_homeworkid;



            string sql_detail_class_homeworkinfo = @" select _student._Sid,_student._Sname,_main_homework._mHid,_main_homework._mHname,_main_homework._mHupdatetime,_student_homework._Hfinishtime,_student_homework._Hscore,_student._SCid,_Cname
                 from _student_homework,_student,_main_homework,_class
                 where _student._Sid in (select _Sid from _student where _SCid='" + str_classid + @"' and _student_homework._Hid='"+str_homeworkid+@"' and _student_homework._Sid=_student._Sid)
                 and _student_homework._Hid=_main_homework._mHid and _student_homework._Sid=_student._Sid and _class._Cid=_student._SCid";   //构造SQL语句  
            data_operate_in_teacher_allhomework.Data_bind(sql_detail_class_homeworkinfo, GridView_detail_class_homework, "_Sid");
     


            //学号   名称    作业ID    作业名称    发布时间  提交时间   得分   班级

            GridView_all_class_homework.Visible = false;//隐藏主表
            Button4.Visible = true;  //激活催交令按钮
        }
    }
    protected void Gridview_detail_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "detaile_select")
        {
            TextBox2.Visible = true;
            TextBox3.Visible = true;  //显示
            TextBox4.Visible = true;
            Button2.Visible = true;
            Label1_student_answer.Visible = true;
            Label_teacher_answer.Visible = true;
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow gvr = GridView_detail_class_homework.Rows[index];
            string str_studentid = ((Label)gvr.FindControl("dLabel_detail_stuid")).Text;    //学生ID
            string str_homeworkid = ((Label)gvr.FindControl("dLabel_detail_homeworkid")).Text;//作业id
            string str_finish_content=data_operate_in_teacher_allhomework.dataread("_student_homework", "_Hfinishcontent", "_Sid", str_studentid, "_Hid", str_homeworkid);//完成的内容
            string true_answer = data_operate_in_teacher_allhomework.dataread("_main_homework", "_mTrueanswer","_mHid", str_homeworkid);//完成的内容
            TextBox2.Text = str_finish_content;   //赋值到作业完成内容文本框
            TextBox4.Text = true_answer;   //赋值到作业完成内容文本框
            extren_stuid.Text=str_studentid;   //把学生ID赋值到隐藏控件上后面使用
            extren_homeid.Text=str_homeworkid;
        }
    }
    protected void Button2_Click(object sender, EventArgs e)    //打分
    {
        string score = TextBox3.Text;
        string sid = extren_stuid.Text;
        string hid = extren_homeid.Text;
        if (data_operate_in_teacher_allhomework.Data_update("_student_homework", "_Hscore", score, "_Sid", sid, "_Hid", hid))
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('打分成功');", true);
        }
        else
            Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('打分失败了::>_<:: ');", true);
        Response.Redirect(Request.RawUrl);  
    }
    protected void Button3_Click(object sender, EventArgs e)   //返回
    {
        TextBox2.Text="";
        TextBox2.Visible=false;
        TextBox4.Text = "";
        TextBox4.Visible = false;
        GridView_detail_class_homework.Visible = false;
        Button2.Visible = false;
        Button4.Visible = false;
        TextBox3.Visible = false;
        GridView_all_class_homework.Visible = true;
        Button6_Click(sender, e);
        Label1_student_answer.Visible = false;
        Response.Redirect(Request.RawUrl);  
    }
    protected void Button4_Click(object sender, EventArgs e)    //催交令
    {
        TextBox1.Visible = true;
        Button5.Visible = true;
        Button6.Visible = true;
    }
    protected void Button5_Click(object sender, EventArgs e)  //发布
    {
        string not_finish_student = "";//未完成作业的学生名单
        if (TextBox1.Text == "" || TextBox1.Text == "说点什么吧。。。催交")
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('催交信息不能为默认或者为空');", true);
        }
        else
        {
            string send_content = TextBox1.Text;   //要发出的消息
            for (int i = 0; i <= GridView_detail_class_homework.Rows.Count - 1; i++)
            {
                GridViewRow gvr = GridView_detail_class_homework.Rows[i];
                string Is_finish = ((Label)gvr.FindControl("dLabel_detail_uptime")).Text;   //查找这个作业完成提交的时间。如果为空则表示没叫    就要催交
                if (Is_finish == "")//没交
                {
                    string _post_id = Make_tran_post_id();
                    not_finish_student = not_finish_student + ((Label)gvr.FindControl("dLabel_detail_stuname")).Text + "-";   //没交作业的都写到这里来
                    string stuid = ((Label)gvr.FindControl("dLabel_detail_stuid")).Text;
                    HttpCookie cookie = Request.Cookies["teaid"];
                    string str_Tid = cookie.Value.ToString();    //获得教师ID
                    string str_Tname = data_operate_in_teacher_allhomework.dataread("_teacher", "_Tname", "_Tid", str_Tid);
                    string str_time = DateTime.Now.ToLocalTime().ToString();//获得时间
                    data_operate_in_teacher_allhomework.dataadd("_tran_post", "_post_id,_addresser_id,_addresser_name,_receiver_id,_post_name,_post_content,_send_time,_read_count", _post_id + "," + str_Tid + ","+ str_Tname + ","+ stuid + "," + "***未交作业提醒消息***" + "," + send_content + "," + str_time + "," + "0");  //发送
                }
            }
            TextBox1.Text = "发布成功：未交作业的" + not_finish_student + " 同学下次登录后将会接受到该消息";

        }

       
    }
    protected void Button6_Click(object sender, EventArgs e)   //关闭
    {
        TextBox1.Text = "说点什么吧。。。催交";
        TextBox1.Visible = false;
        Button5.Visible = false;
        Button6.Visible = false;
    }



    private string Make_tran_post_id()   //生成作业ID
    {

   
        string numA = data_operate_in_teacher_allhomework.dataread("MAX(_post_id)", "_tran_post", true);   //获得站内邮ID
        if (numA == null)
        {
            numA = "00000000000000";
        }
        string numB = "1";   //最大ID 号加1
        numA = numA.Replace(" ", "");

        int lenA = 0, lenB = 0;
        int lenI = 0, intTen = 0;
        int intA = 0, intB = 0;

        StringBuilder result = new StringBuilder("");
        lenA = numA.Length;
        lenB = numB.Length;
        if (lenA > lenB)
            lenI = lenA;
        else
            lenI = lenB;
        for (int i = 0; i < lenI; i++)
        {
            if (i < lenA)
                intA = Convert.ToInt16(numA.Substring(lenA - i - 1, 1));
            else
                intA = 0;
            if (i < lenB)
                intB = Convert.ToInt16(numB.Substring(lenB - i - 1, 1));
            else
                intB = 0;
            int temp = intA + intB + intTen;
            result.Insert(0, Convert.ToString((int)temp % 10));
            intTen = (int)temp / 10;
        }
        if (intTen > 0)
            result.Insert(0, Convert.ToString(intTen));
        return result.ToString();
    }
    protected void gridview_detal_pageindexchanging(object sender, GridViewPageEventArgs e)
    {
        string sql_detail_class_homeworkinfo = @" select _student._Sid,_student._Sname,_main_homework._mHid,_main_homework._mHname,_main_homework._mHupdatetime,_student_homework._Hfinishtime,_student_homework._Hscore,_student._SCid,_Cname
                 from _student_homework,_student,_main_homework,_class
                 where _student._Sid in (select _Sid from _student where _SCid='" + extren_classuid.Text + @"' and _student_homework._Hid='" + extren_classhomeworkid.Text + @"' and _student_homework._Sid=_student._Sid)
                 and _student_homework._Hid=_main_homework._mHid and _student_homework._Sid=_student._Sid and _class._Cid=_student._SCid";   //构造SQL语句  
        data_operate_in_teacher_allhomework.Data_bind(sql_detail_class_homeworkinfo, GridView_detail_class_homework, "_Sid");
     
    }
}